import { delayToast } from "../common";
export interface SingleToast {
    title: string;
    duration: number;
    mask: boolean;
}
export type Toast = string | SingleToast;

// 全局queue
const ToastQueue: Toast[] = [];
//  标记是否正在显示toast
let _isShowingToast = false;

function addToast2Queue(msg: Toast) {
    ToastQueue.push(msg);
    if (!_isShowingToast) {
        showToastFromQueue();
    }
}
async function showToast(msg: Toast) {
    let toastInfo: SingleToast = {
        title: "",
        mask: true,
        duration: 1500,
    };
    if (typeof msg === "string") {
        toastInfo.title = msg;
    } else {
        toastInfo = msg;
    }
    await delayToast(toastInfo.title, toastInfo.mask, toastInfo.duration);
    // 显示队列中的下一个toast
    showToastFromQueue();
}

function showToastFromQueue() {
    if (ToastQueue.length > 0) {
        _isShowingToast = true;
        const msg = ToastQueue.shift();
        showToast(msg!);
    } else {
        _isShowingToast = false;
    }
}

export { addToast2Queue };
